package com.chinadci.smartcommunitysystembackstage.controller;


import com.chinadci.smartcommunitysystembackstage.entity.Clbx;
import com.chinadci.smartcommunitysystembackstage.entity.ScsHandle;
import com.chinadci.smartcommunitysystembackstage.entity.ScsRepair;
import com.chinadci.smartcommunitysystembackstage.service.ScsActivityService;
import com.chinadci.smartcommunitysystembackstage.service.ScsAdminService;
import com.chinadci.smartcommunitysystembackstage.service.ScsHandleService;
import com.chinadci.smartcommunitysystembackstage.service.ScsRepairService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Code Duck
 * @since 2021-04-13
 */
@CrossOrigin
@RestController
@Api("建业E家")
public class ScsRepairController {
    @Autowired
    private ScsRepairService service;

    @Autowired
    private ScsAdminService admin;

    @Autowired
    private ScsHandleService scs;
    //显示所有报修列表
    @ApiOperation(value = "显示所有报修列表")           //swagger
    @ApiImplicitParam(name = "time",value = "报修日期")       //表明要传入的参数
    @RequestMapping(value = "/api/bxfind",method = RequestMethod.GET)
    public Map Find(String time){
        Map<String,Object> map=new HashMap<>();
        List<ScsRepair> list=service.Find(time);
        for (int i = 0; i < list.size(); i++) {
            list.get(i).setBxname(list.get(i).getUser().get(0).getUName());
            if (list.get(i).getScs().size()>0){
                list.get(i).setClzt(list.get(i).getScs().get(0).getHState());
                list.get(i).setClsj(list.get(i).getScs().get(0).getHTime());
                if (list.get(i).getScs().get(0).getAId()!=null)
                list.get(i).setClname(admin.find(list.get(i).getScs().get(0).getAId()).getAName());
            }

        }
        map.put("data",list);
        return map;
    }

    //处理报修
    @ApiOperation(value = "处理报修")           //swagger
    @ApiImplicitParam(name = "Clbx",value = "对象")       //表明要传入的参数
    @RequestMapping(value = "/api/bxupdate",method = RequestMethod.POST)
    public Map Finds(Clbx cl){
        Map<String,Object> map=new HashMap<>();
        if (scs.Find(cl.getRid()).getHState()==0){
            map.put("state",0);     //如果state时0的话证明该报修已经处理过
            return map;
        }
        map.put("state",2);
        map.put("data",scs.Update(cl));
        System.out.println(cl.getAid());
        return map;
    }
}
